import json

from django.shortcuts import render
from rest_framework.views import  APIView
from day10 import models,my_seriailzers
from rest_framework.response import Response
import re,random
# Create your views here.


from utils import jwttool,send,myredis
class Login(APIView):
    def post(self,request):
        phone=request.data.get('phone')
        password=request.data.get('password')


        user=models.UserModel.objects.filter(phone=phone).first()

        if not user:
            return Response({'code':400,'msg':'账号不存在'})
        if password==user.password:
            token=jwttool.tool.get_token(user.id)
            return Response({'code':200,'msg':'登录成功',"data":{'token':token,'userid':user.id}})

        else:
            return Response({'code':400,'msg':'登录失败'})


class Sms_Code(APIView):
    def get(self,request):
        phone=request.query_params.get('phone')
        code=random.randint(1000,9999)
        re_phone=re.match(r'^1[3-9]\d{9}$',phone)
        if not re_phone:
            return Response({'code':400,'msg':'手机号错误'})




        smscode=send.send_message(phone,code)

        jsoncode=json.loads(smscode)
        if jsoncode['statusCode']=='000000':


            #存储到redis中
            myredis.redis_tool.setcode(phone,code)

            return Response({'code': 200, 'msg': '发送成功'})
        else:
            return Response({'code': 400, 'msg': '发送失败'})


class LoginCode(APIView):
    def post(self,request):
        phone=request.data.get('phone')
        code=request.data.get('code')

        re_phone=re.match(r'^1[3-9]\d{9}$',phone)
        if not re_phone:
            return Response({'code':400,'msg':'手机号错误'})


        user=models.UserModel.objects.filter(phone=phone).first()
        if not user:
            return Response({'code':400,'msg':'账号不存在'})
        smscode=myredis.redis_tool.getcode(phone)
        if code==smscode:
            token=jwttool.tool.get_token(user.id)
            return Response({'code':200,'msg':'登录成功',"data":{'token':token,'userid':user.id}})

        else:
            return Response({'code':400,'msg':'登录失败'})


from  django.core.files.storage import default_storage
class Uploads(APIView):
    def post(self,request):
        file=request.data.get('file')

        uploadsfile=default_storage.save(file.name,file)
        url=default_storage.url(uploadsfile)
        return Response({"url":url})


from rest_framework.generics import ListAPIView,CreateAPIView

from rest_framework.pagination import PageNumberPagination
class MyPage(PageNumberPagination):
    page_size_query_param = 'size'

class Add(CreateAPIView):
    queryset = models.JournalismModel.objects.all()
    serializer_class = my_seriailzers.JournalismSer

class Shwo(ListAPIView):
    queryset = models.JournalismModel.objects.all()
    serializer_class = my_seriailzers.JournalismSer
    pagination_class = MyPage

class Detail(APIView):
    def get(self,request):
        id=request.query_params.get('id')
        info=models.JournalismModel.objects.filter(id=id).first()
        ser=my_seriailzers.JournalismSer(info)
        return Response({'code':200,'data':ser.data})

class register(APIView):
    def post(self,request):
        phone=request.data.get('phone')
        password=request.data.get('password')

        user=models.UserModel.objects.filter(phone=phone).first()
        if user:
            return Response({'code':'用户已经注册'})


        data=models.UserModel.objects.create(phone=phone,password=password)
        data.save()
        return Response({'msg':'注册成功'})

